// ActionScript file
import com.xsky.web.WebSrv;

import mx.containers.FormItem;
import mx.controls.Alert;
import mx.controls.DateField;
import mx.controls.TextArea;
import mx.controls.TextInput;

public function createAddPanel(columnTemplate:XML):void
{
	var addPanelTitle:String=columnTemplate.@addPanelTitle;
	addPanel.title=addPanelTitle;
	for each (var item:XML in columnTemplate.column)
	{
		var label:String=item.@label;
		var propertyName:String=item.@name;
		var validator:String=item.@validator;
		var formItem:FormItem=new FormItem();
		formItem.label=label;
		addItemForm.addChild(formItem);
		if (item.@type == "date")
		{
			var dateField:DateField=new DateField();
			dateField.formatString="YYYY-MM-DD";
			dateField.id=propertyName;
			formItem.addChild(dateField);
		}
		else if (item.@type == "string")
		{
			var textInput:TextInput=new TextInput();
			textInput.id=propertyName;
			formItem.addChild(textInput);
		}
		else if (item.@type == "textarea")
		{
			var textArea:TextArea=new TextArea();
			textArea.id=propertyName;
			textArea.width = Number(item.@width);
			textArea.height = Number(item.@height);
			formItem.addChild(textArea);
		}
	}
}

public function resetValues():void
{
	var formItems:Array=addItemForm.getChildren();
	for each (var formitem:* in formItems)
	{
		if (formitem is FormItem)
		{
			for each (var it:* in formitem.getChildren())
			{
				if (it is mx.controls.TextInput || it is mx.controls.TextArea)
				{
					it.text="";
				}
				else if (it is mx.controls.CheckBox)
				{
					it.selected=false;
				}
				else if (it is mx.controls.DateField)
				{
					it.selectedDate=null;
				}
			}
		}

	}
}
public function add():void
{
	
	var dataXml:String = "<data tableName=\""+params["tableName"]+"\">";
	var formItems:Array=addItemForm.getChildren();
	for each (var formitem:* in formItems)
	{
		if (formitem is FormItem)
		{
			for each (var it:* in formitem.getChildren())
			{
				if (it is mx.controls.TextInput || it is mx.controls.TextArea)
				{
					var itemId:String = it.id;
					var value:String = it.text;
					dataXml+="<"+itemId+">"+value+"</"+itemId+">";
				}
				else if (it is mx.controls.CheckBox)
				{
					it.selected=false;
				}
				else if (it is mx.controls.DateField)
				{
					var itemId:String = it.id;
					var value:String = it.text;
					dataXml+="<"+itemId+">"+value+"</"+itemId+">";
				}
			}
		}
	}
	dataXml+="</data>";
	var webSrv:WebSrv = new WebSrv(params["gridService"]);
	var paramObj:Object=new Object();
	paramObj["in0"]=dataXml;
	webSrv.invokeWebSrv("add", paramObj);
	webSrv.onComplete=function(result:String):void
	{
		if(result!=null)
		mx.controls.Alert.show(result);
		search();
	}
}
public function hideAddPanel():void
{
	addPanel.width=0;
	addPanel.visible=false;
}